package org.dromara.utils.listener;

import org.dromara.pe.domain.bo.TaskInfoBo;
import org.dromara.pe.service.ITaskInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Component
public class RedisTaskListener extends KeyExpirationEventMessageListener {
    @Autowired
    private ITaskInfoService iTaskInfoService;

    /**
     * @param listenerContainer 监听器
     */
    public RedisTaskListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();
        // 将拿到的过期键使用之前拼接时的特殊符号分割成字符数组
        if(expiredKey.contains("taskInfo|")){
            String[] expiredKeyArr = expiredKey.split("\\|");
            String businessSign = expiredKeyArr[0].toString();
            Long taskInfoId = Long.parseLong(expiredKeyArr[1].toString());
            if("taskInfo".equals(businessSign)){
                TaskInfoBo taskInfoBo=new TaskInfoBo();
                taskInfoBo.setTaskId(taskInfoId);
                taskInfoBo.setTaskStatus(2L);
                iTaskInfoService.updateByBo(taskInfoBo);
            }
        }
    }
}
